<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FormPanel ref="formPanel1" :list="formList1" :formType="type" @formChange="formChangeHandle1" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button v-show="type != 'show'" type="primary" :click="dataHandle.bind(this, 60341004)">同 意</multiuse-button>
      <multiuse-button v-show="type != 'show'" type="primary" :click="dataHandle.bind(this, 60341005)">驳 回</multiuse-button>
      <el-button @click="closeDrawer">返 回</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getEmployees, queryVinForOrder, queryPotentialCustomer, queryRetailOrderById, auditRetailOrder } from '@/api/dealerSale/SalesSanagement/RetailOrderReview';
import { banWord, IDNumber, checkPassport, CheckSocialCreditCode } from '@/utils/validate';

export default {
  mixins: [dictionary],

  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    this.saveData1 = null;
    return {
      formList: this.createdFrom(),
      formList1: this.createdFrom1(),
      isShowCstmr: false,
      isShowVin: false,
      formData: {},
      editData: {},
      detailData: null,
      cstmrData: null,
      vinData: null
    };
  },
  mounted() {
    this.init();
    // this.queryEmployees();
  },
  methods: {
    init() {
      if (this.type != 'add') {
        this.getInfoById();
      }
      if (this.type == 'show') {
        this.formList.map(x => {
          x.placeholder = '';
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
        this.formList1.map(x => {
          x.placeholder = '';
          if (x.rules) {
            x.rules[0].required = false;
          }
        });
      }
    },
    // 查询销售顾问
    async queryEmployees() {
      const res = await getEmployees();
      if (res.resultCode == 200) {
        res.data.map(x => {
          this.formList.find(item => item.fieldName == 'salesManId').itemList.push({ text: x.userName, value: x.userId });
        });
      }
    },
    // 根据ID查详细
    async getInfoById() {
      const res = await queryRetailOrderById(this.rowData.orderId);
      if (res.resultCode == 200) {
        this.detailData = res.data;
        this.formList.map(x => {
          // 潜客信息替换页面信息vsCustomerDTO 客户编号、客户姓名、手机号、证件类型、证件号、联系地址
          if (x.fieldName == 'customerNo' || x.fieldName == 'customerName' || x.fieldName == 'customerPhone' || x.fieldName == 'cardType' || x.fieldName == 'cardNo' || x.fieldName == 'linkAddress') {
            let data = res.data.vsCustomerDTO;
            if (x.fieldName == 'customerPhone') {
              x.initialValue = data['mobile'];
            } else {
              x.initialValue = data[x.fieldName];
            }
          } else {
            x.initialValue = res.data[x.fieldName];
          }
        });
        this.formList1.map(x => {
          let orderVinShowDto = res.data.orderVinShowDto || {};
          if (x.fieldName == 'salesQuatation' || x.fieldName == 'checkDesc') {
            x.initialValue = res.data[x.fieldName];
          } else if (x.fieldName == 'discountAmount') {
            let result = 0;
            let basicPrice = orderVinShowDto.basicPrice || 0;
            result = parseFloat(basicPrice) - parseFloat(res.data.salesQuatation);
            x.initialValue = result || 0;
          } else {
            x.initialValue = orderVinShowDto[x.fieldName];
          }
        });
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    formChangeHandle1(val) {
      this.saveData1 = val;
    },
    async dataHandle(type) {
      this.formList1.find(x => x.fieldName == 'checkDesc').rules[0].required = type == 60341005 ? true : false;
      this.$refs.formPanel.SUBMIT_FORM();
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.editData = {
          id: this.rowData.orderId || '',
          depositAmount: this.saveData.depositAmount || '',
          depositDate: this.saveData.depositDate || '',
          expectDate: this.saveData.expectDate || '',
          isMajor: this.saveData.isMajor || '',
          isSpecial: this.saveData.isSpecial || '',
          orderType: this.saveData.orderType || '',
          payType: this.saveData.payType || '',
          remark: this.saveData.remark || '',
          salesManId: this.saveData.salesManId || this.detailData.salesManId,
          salesManName: this.saveData.salesManName || this.detailData.salesManName,
          signDate: this.saveData.signDate || '',
          vehicleId: this.vinData ? this.vinData.vehicleId : this.detailData.orderVinShowDto.vehicleId,
          salesQuatation: this.saveData1.salesQuatation || '',
          checkDesc: this.saveData1.checkDesc || '',
          vsCustomerDTO: {
            bussinessId: 181297,
            // bussinessId: this.cstmrData ? this.cstmrData.costomerBusinessId : this.detailData.vsCustomerDTO.bussinessId,
            cardNo: this.saveData.cardNo || '',
            cardType: this.saveData.cardType || '',
            customerName: this.saveData.customerName || '',
            customerNo: this.saveData.customerNo || '',
            mobile: this.saveData.customerPhone || '',
            linkAddress: this.saveData.linkAddress || '',
            potentCustomerId: 351722
            // potentCustomerId: this.cstmrData ? this.cstmrData.potentCustomerId : this.detailData.vsCustomerDTO.potentCustomerId
          },
          orderVinShowDto: {}
        };

        // 潜客信息
        this.editData.vsCustomerDTO = Object.assign(this.editData.vsCustomerDTO, this.saveData);
        // 车辆信息
        this.editData.orderVinShowDto = Object.assign(this.editData.orderVinShowDto, this.saveData1);
        // 保存的数据
        let saveData = Object.assign(this.detailData, this.editData);
        saveData.orderStatus = type;
        const res = await auditRetailOrder([saveData]);

        if (res.resultCode === 200) {
          await this.closeDrawer();
        }
      }
    },
    closeDrawer() {
      this.$emit('close', false, 'refresh');
    },
    // 查询潜客
    cstmrNoHandle() {
      this.isShowCstmr = true;
    },
    closeDialog(val, data) {
      this.isShowCstmr = val;
      this.cstmrData = data[0];
      this.setFormData(data[0]);
      console.log(data, '---关闭潜客---');
    },
    closeDialog1(val, data) {
      this.isShowVin = val;
      this.vinData = data[0];
      this.setFormData(data[0]);
      console.log(data, '---关闭VIN---');
    },
    setFormData(data) {
      for (let key in data) {
        this.formList.map(x => {
          if (x.fieldName == key) {
            x.initialValue = data[x.fieldName];
          }
        });
      }
    },
    // 查询VIN
    vinHandle() {
      this.isShowVin = true;
    },
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '零售信息'
        },
        {
          type: 'INPUT',
          label: '零售单号',
          fieldName: 'orderNo',
          placeholder: '自动生成',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerNo',
          placeholder: '',
          disabled: true,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.cstmrNoHandle()}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '客户姓名',
          fieldName: 'customerName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '手机号',
          fieldName: 'customerPhone',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'cardType',
          placeholder: '请选择',
          itemList: this.createDictList('1508'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '证件号',
          fieldName: 'cardNo',
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'blur' },
            { trigger: 'blur', validator: banWord }
          ],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '联系地址',
          fieldName: 'linkAddress',
          placeholder: '请输入',
          selfCols: 2,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '购车用途',
          fieldName: 'buyPurpose',
          placeholder: '请选择',
          itemList: this.createDictList('1511'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '开单人',
          fieldName: 'sheetManName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: '签约日期',
          fieldName: 'signDate',
          dateType: 'exactdate',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'DATE',
          label: '预交车时间',
          fieldName: 'expectDate',
          dateType: 'exactdate',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售顾问',
          fieldName: 'salesManName',
          placeholder: '',
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '支付方式',
          fieldName: 'payType',
          placeholder: '请选择',
          itemList: this.createDictList('6015'),
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '定金金额',
          fieldName: 'depositAmount',
          maxlength: 9,
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'DATE',
          label: '定金日期',
          fieldName: 'depositDate',
          dateType: 'exactdate',
          disabled: true,
          placeholder: '请选择'
        },
        // {
        //   type: 'SELECT',
        //   label: '大客户订单',
        //   fieldName: 'isMajor',
        //   itemList: this.createDictList('1004'),
        //   disabled: true
        // },
        // {
        //   type: 'SELECT',
        //   label: '特殊订单',
        //   fieldName: 'isSpecial',
        //   itemList: this.createDictList('1004'),
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6026'),
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入',
          selfCols: 3,
          disabled: true
        }
      ];
    },
    createdFrom1() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '',
          disabled: true,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.vinHandle()}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'motorCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '电池号',
          fieldName: 'batteryNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '合格证号',
          fieldName: 'certificateNo',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '品牌名称',
          fieldName: 'brandName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车型名称',
          fieldName: 'modelName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配置名称',
          fieldName: 'packageName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '选装名称',
          fieldName: 'optionName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimColorName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '外色',
          fieldName: 'colorName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售指导价',
          fieldName: 'basicPrice',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售报价',
          fieldName: 'salesQuatation',
          placeholder: '请输入',
          disabled: true,
          onInput: val => {
            console.log(val, '---销售报价---');
            let result = 0;
            let basicPrice = this.formList1.find(x => x.fieldName === 'basicPrice').initialValue || 0;
            result = parseFloat(basicPrice) - parseFloat(val);
            this.formList1.find(x => x.fieldName === 'discountAmount').initialValue = result || 0;
          }
        },
        {
          type: 'INPUT',
          label: '折让金额',
          fieldName: 'discountAmount',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'TEXT_AREA',
          label: '审批意见',
          fieldName: 'checkDesc',
          selfCols: 3,
          placeholder: '',
          rules: [{ required: false, message: '请输入审批意见', trigger: 'blur' }]
        }
      ];
    }
  }
};
</script>
